package com.hgy.crowd.handler;

import com.hgy.crowd.api.MySQLRemoteService;
import com.hgy.crowd.constant.CrowdConstant;
import com.hgy.crowd.service.ProjectService;
import com.hgy.crowd.util.ResultEntity;
import com.hgy.crowd.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author He
 * @Date 2020/7/4 21:05
 */
@Controller
public class ProjectConsumerHandler {
    @Resource
    private ProjectService projectService;
    @Resource
    private MySQLRemoteService mySQLRemoteService;
    @RequestMapping("/create/project/information")
    public String saveProjectBasicInfo(ProjectVO projectVO,
                                       MultipartFile headerPicture,
                                       List<MultipartFile> detailPictureList,
                                       HttpSession session,
                                       ModelMap modelMap
    ) {
        ResultEntity resultEntity = projectService.uploadHeaderPicture(headerPicture, projectVO);
        ResultEntity resultListEntity = projectService.uploadDetailPicList(detailPictureList, projectVO);
        if (ResultEntity.FAILED.equals(resultEntity.getResult())) {
            modelMap.addAttribute("message", "头图上传失败，请检查后重新上传");
            return "project-launch";
        } else if (ResultEntity.FAILED.equals(resultListEntity.getResult())) {
            modelMap.addAttribute("message","详情图上传失败，请检查后重新上传");
            return "project-launch";
        } else {
            session.setAttribute("tempProject", projectVO);
            return "redirect:http://localhost/project/return/info/page";
        }
    }
    @ResponseBody
    @RequestMapping("/create/upload/return/picture.json")
    public ResultEntity uploadReturnPic(@RequestParam(value = "returnPicture") MultipartFile returnPicture){
        ResultEntity resultEntity=projectService.uploadReturnPic(returnPicture);
        return resultEntity;
    }
    @ResponseBody
    @RequestMapping("/create/save/return.json")
    public ResultEntity saveReturnInfo(ReturnVO returnVO,HttpSession session){
        try {
            ProjectVO tempProject = (ProjectVO) session.getAttribute("tempProject");
            List<ReturnVO> returnVOList = tempProject.getReturnVOList();
            if (returnVOList==null||returnVOList.size()==0){
                returnVOList=new ArrayList<>();
                tempProject.setReturnVOList(returnVOList);
            }
            returnVOList.add(returnVO);
            System.out.println(tempProject);
            session.setAttribute("tempProject",tempProject);
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }
        return ResultEntity.successWithoutData();
    }
    @RequestMapping("/create/confirm")
    public String saveConfirmInfo(HttpSession session, MemberConfirmInfoVO memberConfirmInfoVO,ModelMap modelMap){
        ProjectVO tempProject = (ProjectVO) session.getAttribute("tempProject");
        if (tempProject==null){
            throw new RuntimeException("");
        }
        tempProject.setMemberConfirmInfoVO(memberConfirmInfoVO);
        MemberLoginVO memberLoginVO=(MemberLoginVO) session.getAttribute(CrowdConstant.MEMBER_LOGGING_KEY);
        Integer memberId = memberLoginVO.getId();
        ResultEntity<String> resultEntity=mySQLRemoteService.saveProjectVORemote(tempProject,memberId);
        if (ResultEntity.FAILED.equals(resultEntity.getResult())){
           modelMap.addAttribute("message",resultEntity.getMessage());
           return "project-confirm";
        }
        session.removeAttribute("tempProject");
        return "redirect:http://localhost/project/create/success";
    }

    @RequestMapping("/get/project/detail/{projectId}")
    public String getProjectDetail(@PathVariable("projectId") Integer projectId, Model model) {
        ResultEntity<DetailProjectVO> resultEntity = mySQLRemoteService.getDetailProjectVORemote(projectId);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            DetailProjectVO detailProjectVO = resultEntity.getData();
            model.addAttribute("detailProjectVO", detailProjectVO);
        }
        return "project-show-detail";
    }
}
